package com.kgcx.schedule.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.kgcx.common.bean.ReturnData;
import com.kgcx.core.exception.BusinessException;
import com.kgcx.dbutils.core.model.ParameterMap;
import com.kgcx.schedule.domain.ScheduleRecordDomain;
import com.kgcx.schedule.service.BaseBusinessService;
import com.kgcx.schedule.service.ScheduleRecordService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(value = "派单记录接口", tags = "派单记录接口")
@RestController
@RequestMapping(value = "/v1/schedule/record")
public class ScheduleRecordController extends BaseBusinessController<String, ScheduleRecordDomain> {
    @Resource
    private ScheduleRecordService scheduleRecordService;

    @Override
    protected BaseBusinessService<String, ScheduleRecordDomain> getService() {
        return scheduleRecordService;
    }

    @ApiOperation(value = "查询派单记录列表", httpMethod = "GET", notes = "查询派单记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class), //
            @ApiImplicitParam(name = "pageNum", value = "第几页，从1开始编号", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class), //
            @ApiImplicitParam(name = "orderBy", value = "排序，比如“city_id, order_type, create_time desc”", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
            @ApiImplicitParam(name = "city_id", value = "城市ID", required = true, paramType = "query", dataType = "Long", dataTypeClass = Long.class), //
            @ApiImplicitParam(name = "order_no_like", value = "订单号（模糊查询）", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
            @ApiImplicitParam(name = "order_type", value = "订单类型：1-立即接机、2-立即接站、3-预约接机、4-预约接站、5-预约送机、6-预约送站、7-预约用车、8-立即用车", required = false, allowableValues = "1,2,3,4,5,6,7,8", paramType = "query", dataType = "int", dataTypeClass = Integer.class), //
            @ApiImplicitParam(name = "auto_type", value = "用车类型：1-公务轿车、2-豪华型、3-商务七座", required = false, allowableValues = "1,2,3", paramType = "query", dataType = "int", dataTypeClass = Integer.class), //
            @ApiImplicitParam(name = "placing_user_phone_like", value = "下单人手机号（模糊查询）", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
            @ApiImplicitParam(name = "passenger_phone_like", value = "乘车人手机号（模糊查询）", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
            @ApiImplicitParam(name = "driver_name_like", value = "司机姓名（模糊查询）", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
            @ApiImplicitParam(name = "driver_phone_like", value = "司机手机号（模糊查询）", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
            @ApiImplicitParam(name = "schedule_status", value = "派单状态：101-待派单、102-预派单、103-已派单、104-待推送、105-已推送、106-已结束、107-已取消、108-派单失败", required = false, allowableValues = "101,102,103,104,105,106,107,108", paramType = "query", dataType = "int", dataTypeClass = Integer.class), //
            @ApiImplicitParam(name = "schedule_status_in", value = "派单状态（多选），用英文逗号“,”拼接的状态ID字符串，比如“101,103”", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
            @ApiImplicitParam(name = "create_time_begin", value = "下单时间（起始），格式为：YYYY-MM-ddTHH:mm:ss+08:00", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
            @ApiImplicitParam(name = "create_time_end", value = "下单时间（结束），格式为：YYYY-MM-ddTHH:mm:ss+08:00", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
    })
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ReturnData<List<ScheduleRecordDomain>> list() {
        ParameterMap params = $params();
        List<ScheduleRecordDomain> items = getService().selectEntityListPage(params);
        ReturnData<List<ScheduleRecordDomain>> result = new ReturnData<List<ScheduleRecordDomain>>();
        result.setData(items);
        result.setTotal(params.getTotalCount());
        result.setPages(params.getTotalPage());
        result.setPageNum(params.getPageNo());
        return result;
    }

    @ApiOperation(value = "查询派单记录详情", httpMethod = "GET", notes = "查询派单记录详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order_no", value = "订单号", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class) //
    })
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public ReturnData<ScheduleRecordDomain> get() {
        return new ReturnData<ScheduleRecordDomain>(getService().selectEntity($("order_no")));
    }

    @ApiOperation(value = "派单记录改派司机（手动派单）", httpMethod = "POST", notes = "派单记录改派司机（手动派单）。注意：record对象中只需传入order_no和driver_id字段。")
    @RequestMapping(value = "/changeDriver", method = RequestMethod.POST)
    public ReturnData<ScheduleRecordDomain> changeDriver(
            @ApiParam(name = "record", value = "record对象中只需传入order_no和driver_id字段。", required = true) @RequestBody ScheduleRecordDomain record //
    ) {
        try {
            ScheduleRecordDomain domain = scheduleRecordService.changeDriver(record.getOrder_no(),
                    record.getDriver_id(), 0L); // TODO 操作员ID暂时写死成0
            return new ReturnData<ScheduleRecordDomain>(domain);
        } catch (BusinessException e) {
            return new ReturnData<ScheduleRecordDomain>(String.valueOf(e.getCode()), e.getMessage());
        }
    }
}
